package com.example.gokuplayalong.bases.permission;

import android.Manifest;
import android.os.Build;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;

import com.example.gokuplayalong.MainActivity;
import com.example.gokuplayalong.bases.exception.ExceptionUtils;
import com.kongzue.dialogx.dialogs.MessageDialog;
import com.kongzue.dialogx.interfaces.OnDialogButtonClickListener;

import java.util.List;

import pub.devrel.easypermissions.EasyPermissions;

public class PermissionUtils {
    public final static int requestCode_camera = 1;
    public final static int requestCode_storage = 2;
    public final static int requestCode_storage_sdk33 = 3;
    public final static int requestCode_READ_CLIPBOARD = 3;
    public static String[] permission_camera = {Manifest.permission.CAMERA};
    public static String[] permission_storage = {Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE};

    @RequiresApi(api = Build.VERSION_CODES.TIRAMISU)
    public static String[] permission_storage_sdk33 = {Manifest.permission.READ_MEDIA_IMAGES};
    public static String[] permission_install_package = {Manifest.permission.INSTALL_PACKAGES};
    public static String[] permission_camera_storage = {Manifest.permission.CAMERA, Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE};


    static PermissionUtils INSTANCE;

    public static PermissionUtilsInterface permissionUtilsInterface;

    public static PermissionUtils getInstance() {
        if (INSTANCE == null) INSTANCE = new PermissionUtils();
        return INSTANCE;
    }

    public void checkPermission(String[] perms, String rationale, int requestCode, PermissionUtilsInterface permissionUtilsInterface) {
        PermissionUtils.permissionUtilsInterface = permissionUtilsInterface;
        if (!EasyPermissions.hasPermissions(MainActivity.getInstance(), perms)) {
            MessageDialog.show("权限申请提示", rationale, "继续", "取消").setOkButtonClickListener(new OnDialogButtonClickListener<MessageDialog>() {
                @Override
                public boolean onClick(MessageDialog dialog, View v) {
                    try {
                        EasyPermissions.requestPermissions(MainActivity.getInstance(), rationale, requestCode, perms);

                    } catch (Throwable e) {
                        ExceptionUtils.getException(ExceptionUtils.getTraceInfo(), e);
                    }
                    return false;
                }
            });
        }
        else {
            PermissionUtils.permissionUtilsInterface.hasPermissions();
        }
    }

    public void Test() {
        PermissionUtils.getInstance().checkPermission(
                PermissionUtils.permission_storage, "\"悟空陪玩\"需要申请存储权限，用于保存缓存文件、安装包", PermissionUtils.requestCode_storage, new PermissionUtilsInterface() {
                    @Override
                    public void onPermissionsGranted(int i, @NonNull List<String> list) {

                    }

                    @Override
                    public void onPermissionsDenied(int i, @NonNull List<String> list) {

                    }

                    @Override
                    public void hasPermissions() {

                    }
                });
    }
}
